<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>04-文字-显示方位-避免重叠</title>
  <style>
    body {
      background-color: #000;
      text-align: center;
    }

    canvas {
      background-color: #fff;
    }

    #map {
      width: 800px;
      height: 800px;
      margin: 0 auto;
      overflow: auto;
    }
  </style>
</head>

<body>
  <div id="map"></div>


  <script>
    let canvas = null
    let context = null
    let width = 200
    let height = 200
    let realWidthPre = 0
    window.onload = function() {
      const canvasDivId = 'map'
      const canvasWidth = 1200
      const canvasHeight = 800
      const canvasParams = [canvasDivId, canvasWidth, canvasHeight]
      initCanvas(canvasParams)

      const startX = 200
      const startY = 200

      const dist = 260

      context.beginPath()
      context.moveTo(startX, startY)
      context.lineTo(startX, startY + 400)
      context.stroke()
      context.beginPath()
      context.moveTo(startX + dist, startY)
      context.lineTo(startX + dist, startY + 400)
      context.stroke()
      context.beginPath()
      context.moveTo(startX, startY + 200)
      context.lineTo(startX + dist, startY + 200)
      context.stroke()



      // setTextDirection(context, {
      //   x: startX,
      //   y: startY + 200,
      //   rightTop: '管底名义坡降',
      //   rightBottom: '管底名义坡降',
      //   font: '40px sans-serif'
      // })
      // setTextDirection(context, {
      //   x: startX,
      //   y: startY + 200,
      //   text: 'hello world',
      //   direction: 'right-bottom',
      //   font: '40px sans-serif'
      // })
      // setTextDirection(context, {
      //   x: startX + 400,
      //   y: startY + 200,
      //   text: '水力名义坡降',
      //   direction: 'left-top',
      //   font: '40px sans-serif'
      // })
      // setTextDirection(context, {
      //   x: startX + 400,
      //   y: startY + 200,
      //   text: 'everyday is great',
      //   direction: 'left-bottom',
      //   font: '40px sans-serif'
      // })
      const params = {
        start: {
          x: startX,
          y: startY + 200,
          top: '管底名义坡降',
          bottom: '管底名义坡降'
        },
        end: {
          x: startX + dist,
          y: startY + 200,
          top: '管底名义坡降',
          bottom: '管底名义坡降'
        },
        font: '12px sans-serif',
        margin: 5
      }
      const arr = calcTextOverlap(context, params)
      arr.forEach(item => {
        setTextDirection(context, item)
      })
    }

    /**
     * 初始化canvas 信息
     */
    function initCanvas([canvasDomId, canvasWidth, canvasHeight]) {
      const canvasParent = document.getElementById(canvasDomId)
      if (canvas) {
        canvas.parentNode.removeChild(canvas)
      }
      canvas = document.createElement("canvas")
      canvasParent.appendChild(canvas)
      width = canvasWidth
      height = canvasHeight
      canvas.width = width
      canvas.height = height
      context = canvas.getContext("2d")
    }
    /**
     * 计算文本重叠
    */
    function calcTextOverlap(ctx, params) {      
      params = Object.assign({
        start: null,
        end: null,
        font: '10px sans-serif',
        margin: 5
      }, params);
      context.beginPath()
      context.font = params.font
      const topSize = ctx.measureText(params.start.top + params.end.top)
      const topHeight = topSize.actualBoundingBoxAscent - topSize.actualBoundingBoxDescent
      const topDist = params.end.x - params.start.x - topSize.width - params.margin
      let leftTopHeight = 0
      if (topDist < 0) {
        leftTopHeight = topSize.actualBoundingBoxAscent + params.margin
      }
      const bottomSize = ctx.measureText(params.start.bottom + params.end.bottom)
      const bottomHeight = bottomSize.actualBoundingBoxAscent - bottomSize.actualBoundingBoxDescent
      const bottomDist = params.end.x - params.start.x - bottomSize.width - params.margin
      let leftBottomHeight = 0
      if (bottomDist < 0) {
        leftBottomHeight = bottomSize.actualBoundingBoxAscent + params.margin
      }

      return [
        {
          x: params.start.x,
          y: params.start.y,
          text: params.start.top,
          direction: 'right-top',
          font: params.font
        },
        {
          x: params.end.x,
          y: params.end.y - leftTopHeight,
          text: params.end.top,
          direction: 'left-top',
          font: params.font
        },
        {
          x: params.start.x,
          y: params.start.y,
          text: params.start.bottom,
          direction: 'right-bottom',
          font: params.font
        },
        {
          x: params.end.x,
          y: params.end.y + leftBottomHeight,
          text: params.end.bottom,
          direction: 'left-bottom',
          font: params.font
        }
      ]
    }

    /**
     * 设置文本显示位置
    */
    function setTextDirection(ctx, params) {
      params = Object.assign({
        text: '',
        x: null,
        y: null,
        margin: 5,
        font: '10px sans-serif',
        color: 'black',
        style: 'fill', // 'fill' 'stroke', 填充 边框
        direction: 'left-top' // 'left-top' 'left-center' 'left-bottom' 'center-top' 'center-center' 'center-bottom' 'right-top' 'right-center' 'right-bottom', 左上 左中 左下 中上 中中 中下 右上 右中 右下
      }, params);

      if (!ctx || params.text === '' || !params.x || !params.y) return

      ctx.font = params.font
      const size = ctx.measureText(params.text)
      const height = size.actualBoundingBoxAscent - size.actualBoundingBoxDescent
      switch (params.direction) {
        case 'left-top':
          params.x = params.x - size.width - params.margin
          params.y = params.y - params.margin
          break
        case 'left-center':
          params.x = params.x - size.width - params.margin
          params.y = params.y + height / 2
          break
        case 'left-bottom':
          params.x = params.x - size.width - params.margin
          params.y = params.y + height + params.margin
          break
        case 'center-top':
          params.x = params.x - size.width / 2
          params.y = params.y - height
          break
        case 'center-center':
          params.x = params.x - size.width / 2
          params.y = params.y + height / 2
          break
        case 'center-bottom':
          params.x = params.x - size.width / 2
          params.y = params.y + height
          break
        case 'right-top':
          params.x = params.x + params.margin
          params.y = params.y - params.margin
          break
        case 'right-center':
          params.x = params.x + params.margin
          params.y = params.y + height / 2
          break
        case 'right-bottom':
          params.x = params.x + params.margin
          params.y = params.y + height + params.margin
          break
      }
      ctx.beginPath()
      ctx.fillStyle = params.color
      if (params.style === 'fill') {
        ctx.fillText(params.text, params.x, params.y)
      } else {
        ctx.strokeText(params.text, params.x, params.y)
      }
    }
  </script>
</body>

</html>